package com.arpa.wms.cancel.delivery;

import cn.hutool.json.JSONUtil;
import com.arpa.wms.cancel.DeliveryCancelHandlerTypeConstants;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.entity.Delivery;
import com.arpa.wms.domain.enums.DeliveryStatusEnum;
import com.arpa.wms.service.IDeliveryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 已分配订单取消处理
 * 1、更新订单状态作废；
 * 2、分配库存退回原库位；
 *
 * @author leo
 */
@Log4j2
@Component(DeliveryCancelHandlerTypeConstants.DISTRIBUTIONED)
public class DistributionedCancelHandler extends AbstractDeliveryCancelHandler {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handle(IDeliveryService deliveryService, Delivery delivery) {
        log.info("已分配、部分分配订单取消处理。订单信息：{}", JSONUtil.toJsonStr(delivery));
        log.info("已分配、部分分配订单取消【step1】：更新订单作废");
        boolean bool = deliveryService.update(Wrappers.lambdaUpdate(Delivery.class)
                .eq(Delivery::getId,delivery.getId())
                .set(Delivery::getIsCancel, NumConst.NUM_ONE)
                .set(Delivery::getStatus, DeliveryStatusEnum.VOID.getValue())
        );
        log.info("已分、部分分配配订单取消【step2】：分配库存退回原库位");
        handleInventory(delivery,true);
        return bool;
    }

}
